$(function (){

var students
var pageSize
var curPage
var maxPage
if ($("#allPage").val()==null){
    getAllStudent()
}else {
    getPageByUrl()
    changePage(curPage,pageSize)
}
  function getPageByUrl(){
      var url=window.location.href
      var url1=url.split("curPage=")[1];
      url1=url1.split("pageSize=")
      curPage=parseInt(url1[0]);
      pageSize=parseInt(url1[1]);
      console.log(curPage,pageSize)
  }

    function getAllStudent () {
        $.ajax({
            url: "/ListStudent",
            type: "Post",
            data: {},
            success: function (data) {
                var students = JSON.parse(data)
                $(".dlt").remove()
                for (s of students) {
                    var tr = "<tr class='dlt'> "
                    for (var i in s) {
                        if (i != "photopath") {
                            tr += "<td>" + s[i] + "</td>"
                        } else {
                            if(s.gender=="女")
                                s[i]="../img/girl.jpg"
                            else
                                s[i]="../img/boy.jpg"
                            tr += "<td><img src='" + s[i] + "'style='width: 100px;height: 100px'/></td>"
                        }
                    }

                    tr += "<td><a class='delete'>删除</a><a>&nbsp;</a> <a class='update' >更新</a></td>"
                    tr += "</tr>"
                    console.log(tr)
                    $("#students tr:last").after(tr)
                }
                $(".delete").click(function (){
                    deleteStudentByName($(this).parent().siblings()[0].innerText)
                })
                $(".update").click(function (){
                    window.location.href="modifyInfo.html?stuNo="+$(this).parent().siblings()[0].innerText
                })
            },
            error: function () {
                console.log("出现错误！")
            }
        })
    }
    function changePage(curPage,pageSize){
        console.log(curPage,pageSize)

        $(".dlt").remove()
        $.ajax({
            url:"/ListStudentByPage",
            type:"Post",
            data: "curPage="+curPage+"&pageSize="+pageSize,
            async:false,
            success:function (data){
                 console.log(data)
                var jsonData=JSON.parse( data );
                maxPage=jsonData.Maxpage

                if(jsonData.students==null){
                alert("页面跳转错误！")
            }else{
                students=jsonData.students
                for(var  s of students) {
                var tr = "<tr class='dlt'>"
                for (var i in s) {
                if (i != "photopath") {
                tr += "<td>" + s[i] + "</td>"
                 } else {
                    if(s.gender=="女")
                        s[i]="../img/girl.jpg"
                    else
                        s[i]="../img/boy.jpg"
                     tr += "<td><img src='" + s[i] + "'style='width: 100px;height: 100px'/></td>"
                    }
                 }
                    tr += "<td><a class='delete'>删除</a><a>&nbsp;</a> <a class='update' >更新</a></td>"
                    tr += "</tr>"
                    $("#students tr:last").after(tr)
                }

                $(".delete").click(function (){
                    deleteStudentByName($(this).parent().siblings()[0].innerText)
                })
                    $(".update").click(function (){
                        window.location.href="modifyInfo.html?stuNo="+$(this).parent().siblings()[0].innerText
                    })
                }
            },
            error:function (){
              console.log("出现错误！")
            }
            })
    }


    function search(){
        $(".dlt").remove()
        var stuName=$("#userName").val()
        alert(stuName)
        if (stuName==null){
            alert("不能为空")
            return
        }
        $.ajax({
            url: "/GetStudentByName?stuName="+$("#userName").val(),
            type: "Post",
            data: {},
            success: function (data) {
                var students = JSON.parse(data)
                for (s of students) {
                    var tr = "<tr class='dlt'>"
                    for (var i in s) {
                        if (i != "photopath") {
                            tr += "<td>" + s[i] + "</td>"
                        } else {
                            if(s.gender=="女")
                                s[i]="../img/girl.jpg"
                            else
                                s[i]="../img/boy.jpg"
                            tr += "<td><img src='" + s[i] + "'style='width: 100px;height: 100px'/></td>"
                        }
                    }
                    tr += "<td><a class='delete'>删除</a><a>&nbsp;</a> <a class='update' >更新</a></td>"
                    tr += "</tr>"
                    console.log(tr)
                    $("#students tr:last").after(tr)
                }
                $(".delete").click(function (){
                    deleteStudentByName($(this).parent().siblings()[0].innerText)

                })
                $(".update").click(function (){
                    window.location.href="modifyInfo.html?stuNo="+$(this).parent().siblings()[0].innerText
                })
            },
            error: function () {
                console.log("出现错误！")
            }
        })
    }
    $("#submit").click(search)
    function deleteStudentByName(stuNo){
        deleteStudent(stuNo)
        if ($("#allPage").val()==null){
            getAllStudent()
        }else {
            getPageByUrl()
            changePage(curPage,pageSize)
        }
    }
    function deleteStudent(stuNo){
        $.ajax({
            url:"/DeleteStudentByNo?stuNo="+stuNo,
            type:"Post",
            data: {},
            async:false,//同步
            success:function (data){
                console.log(data)
                var jsonData=JSON.parse( data );
                if(jsonData.msg=="ok"){
                    alert(jsonData.result)
                }else {
                    alert(jsonData.result)
                }
            },
            error:function (){
                console.log("出现错误！")
            }
        })
}
    $("#firstPage").click(function (){
    curPage=1;
    window.location.href="listStudentByPage.html?curPage="+curPage+"&pageSize="+pageSize
})
    $("#prevPage").click(function (){
    curPage-=1;
    if(curPage<=1){
        curPage=1
    }
    window.location.href="listStudentByPage.html?curPage="+curPage+"&pageSize="+pageSize
})
    $("#nextPage").click(function (){
    curPage+=1
    if(curPage>=maxPage){
        curPage=maxPage
    }
    window.location.href="listStudentByPage.html?curPage="+curPage+"&pageSize="+pageSize

})
    $("#lastPage").click(function (){
    curPage=maxPage
    window.location.href="listStudentByPage.html?curPage="+curPage+"&pageSize="+pageSize

})
    $("#submit").click(function (){

})
    $("#allPage").click(function (){
    window.location.href="listStudent.html"
})
    $("#addStduent").click(function (){
    window.location.href="modifyInfo.html"
})
    $("#ByPage").click(function (){
    window.location.href="listStudentByPage.html?curPage=1&pageSize=10"
})
})
